package manage.controller;

import manage.domain.User;
import manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ModelAndView login(String username,String password){
        User current_user=this.userService.login(username,password,request);

        if(current_user!=null){
            super.request.getSession().setAttribute("current_user",current_user);
            return new ModelAndView("redirect:/index");
        }

        return new ModelAndView("login")
                .addObject("msg","用户不存在|密码错误|权限不足");

    }


    @GetMapping("/detail")
    public ModelAndView detail(Integer id){
        return new ModelAndView("user/detail")
                .addObject("user",this.userService.detail(id));
    }

    @GetMapping("/remove")
    public String remove(Integer id) {
        this.userService.remove(id);
        return "redirect:/user/index";

    }
    @PostMapping("/disabled")
    public String disabled(User user){
        this.userService.disabled(user);
        return "redirect:/user/index";
    }

    @GetMapping("/disabled")
    public ModelAndView disabled(Integer id){
        return new ModelAndView("user/modifyPass")
                .addObject("user",this.userService.detail(id));
    }

    @GetMapping("/append")
    public String append(){
        return "user/append";
    }

    @PostMapping("/append")
    public String append(User user, MultipartFile file){
        this.userService.append(user,file);
        return "redirect:/user/index";
    }

    @RequestMapping({"/", "/index"})
    public ModelAndView index(User condition,  @RequestParam(value = "page", defaultValue = "1") Integer page) {
        return new ModelAndView("user/index")
                .addObject("condition", condition)
                .addObject("pager", this.userService.list(condition,page));
    }
    @ResponseBody
    @GetMapping("/isExistByUserName")
    public boolean isExistByBrandName(String username){
        return  this.userService.isExistByUserName(username);
    }



}
